{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Convert Tubes To Images"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook contains a few examples of how to call wrapped methods in itk and ITKTubeTK."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "ITK, ITKTubeTK, and ITKWidgets must be installed on your system for this notebook to work."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import itk\n",
    "from itk import TubeTK as ttk\n",
    "from itkwidgets import view"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load the tubes and a reference image which provides the size, spacing, origin, and orientation for the desired output image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "PixelType = itk.F\n",
    "Dimension = 3\n",
    "ImageType = itk.Image[PixelType, Dimension]\n",
    "    \n",
    "# Read tre file\n",
    "TubeFileReaderType = itk.SpatialObjectReader[Dimension]\n",
    "    \n",
    "tubeFileReader = TubeFileReaderType.New()\n",
    "tubeFileReader.SetFileName(\"Data/MRI-Normals/Normal071-VascularNetwork.tre\")\n",
    "tubeFileReader.Update()\n",
    "\n",
    "tubes = tubeFileReader.GetGroup()\n",
    "\n",
    "\n",
    "# Read template image\n",
    "TemplateImageType = itk.Image[PixelType, Dimension]\n",
    "TemplateImageReaderType = itk.ImageFileReader[TemplateImageType]\n",
    "    \n",
    "templateImageReader = TemplateImageReaderType.New()\n",
    "templateImageReader.SetFileName(\"Data/MRI-Normals/Normal071-MRA.mha\")\n",
    "templateImageReader.Update()\n",
    "\n",
    "templateImage = templateImageReader.GetOutput()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Visualize the template image, just because it looks cool - the data in the image is actually irrelevant."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e33543bccfa3480d8aaf4b5021abd21b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageF3; pro…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "view(templateImage)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a binary image that represents the spatial extent of the TubeSpatialObjects in the hierarchy of SpatialObjects in the variable \"tubes\" that was read-in above.   If you only want to visualize centerlines of the tubes, set \"UseRadius\" to false."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "itkImageF4 not loaded from module TubeTK because of exception:\n",
      " module 'itk.TubeTKPython' has no attribute 'itkImageF4'\n",
      "vectoritkImageF4 not loaded from module TubeTK because of exception:\n",
      " module 'itk.TubeTKPython' has no attribute 'vectoritkImageF4'\n"
     ]
    }
   ],
   "source": [
    "TubesToImageFilterType = ttk.ConvertTubesToImage[TemplateImageType]\n",
    "tubesToImageFilter = TubesToImageFilterType.New()\n",
    "tubesToImageFilter.SetUseRadius(True)\n",
    "tubesToImageFilter.SetTemplateImage(templateImageReader.GetOutput())\n",
    "tubesToImageFilter.SetInput(tubes)\n",
    "tubesToImageFilter.Update()\n",
    "\n",
    "outputImage = tubesToImageFilter.GetOutput()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Visualize the results by blending the template and output images.  Again, the content of the template image\n",
    "doesn't actually matter, but since these tubes were generated from the content of the template image, blending them illustrates how well the binary tube image corresponds with their source image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7c11a4f6da5941af90e0bcd9cac1555f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Viewer(geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePython.itkImageF3; pro…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "TTKImageMathType = ttk.ImageMath[ImageType,ImageType]\n",
    "\n",
    "imMath = TTKImageMathType.New(Input = outputImage)\n",
    "imMath.AddImages(templateImage, 2048, 1)\n",
    "combinedImage = imMath.GetOutput()\n",
    "view(combinedImage)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "WheelEnv",
   "language": "python",
   "name": "wheelenv"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
